# wzt
from flask import Flask, jsonify, request, render_template,make_response
import requests

app = Flask(__name__)

students = [
    {'id': 1, 'name': '张三', 'gender': '男', 'age': 18,
     'id_card': '123456789', 'phone': '12345678901', 'address': '北京'},
    {'id': 2, 'name': '李四', 'gender': '男', 'age': 19,
     'id_card': '123456780', 'phone': '12345678902', 'address': '上海'},
    {'id': 3, 'name': '王五', 'gender': '女', 'age': 20,
     'id_card': '123456781', 'phone': '12345678903', 'address': '广州'}
]

@app.route('/') # 获取学生信息页面
def index():
    return render_template('index.html')

@app.route('/addStu',methods=['post']) # 添加学生信息页面
def addStu():
    stu = eval(request.data.decode('utf-8'))

    stu['id'] = int(stu['id'])
    students.append(stu)
    return 'haha'

@app.route('/delStu/<int:student_id>', methods=['DELETE'])
def delete_student(student_id):

    print(students)
    # 找到对应的学生并删除
    student_to_delete = None
    for student in students:
        print(student['id'])
        if student['id'] == student_id:
            student_to_delete = student
            print(student_to_delete)
            break

    if student_to_delete:
        students.remove(student_to_delete)
        return jsonify({'message': 'Student deleted successfully'}), 200
    else:
        return jsonify({'message': 'Student not found'}), 404
    

@app.route('/editStu/<int:student_id>', methods=['PUT'])
def edit_student(student_id):

    # 找到要更新的学生记录
    student_to_update = None
    for student in students:
        if student['id'] == student_id:
            student_to_update = student
            print(student_to_update)
            break

    stu = eval(request.data.decode('utf-8'))

    student_to_update.update(stu)

    student_to_update['id'] = int(student_to_update['id'])

    return jsonify({'message': 'Student edit successfully'}), 200



@app.route('/students') # 获取学生信息页面
def getStudents():
    return jsonify(students)

if __name__ == '__main__':
    app.run(debug=True,host='0.0.0.0',port=80)


